package E2017;
public class Exam3 implements Runnable {
    static Integer tickets = 20;
    @Override
    public void run() {
        while (true){
            synchronized (tickets){
                if (tickets>0){
                    System.out.println(Thread.currentThread().getName()+"正在出售第"+tickets--+"张票！");
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
                if (tickets==0){
                    System.exit(1);
                }
            }
        }

    }
    public static void main(String[] args) {
        Exam3 e = new Exam3();
        new Thread(e,"窗口1").start();
        new Thread(e,"窗口2").start();
        new Thread(e,"窗口3").start();
    }
}